package com.saasitems.admin.web.restcontroller;

import com.saasitems.admin.dao.OriginalMapper;
import com.saasitems.admin.model.Register;
import com.saasitems.admin.service.MattsService;
import com.saasitems.admin.service.OriginalService;
import com.saasitems.admin.service.RegisterService;
import com.saasitems.admin.service.StarService;
import com.saasitems.admin.web.vo.rest.matts.RestMattsInfoVO;
import com.saasitems.admin.web.vo.rest.original.RestOriginalInfoVO;
import com.saasitems.admin.web.vo.rest.star.RestStarInfoVO;
import com.saasitems.common.utils.AuthUtil;
import com.saasitems.common.utils.IpUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/19.
 */
@RestController
public class RestInfoController {
    protected Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private OriginalService originalService;

    @Autowired
    private MattsService mattsService;

    @Autowired
    private StarService starService;

    @Autowired
    private RegisterService registerService;


    @RequestMapping(value = "rest/original/post/{classId}", method = RequestMethod.POST)
    @ResponseBody
    public Object getOriginal(@PathVariable("classId") String classId) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (!AuthUtil.newInstance().isOk()) {
            logger.error("试用已到期");
            ret.put("status", "error");
            ret.put("info", null);
            return ret;
        }

        try {
            ret.put("status", "success");
            List<RestOriginalInfoVO> info = originalService.getInfoByClassId(classId);
            ret.put("info", info);
        } catch (Exception e) {
            ret.put("status", "error");
            ret.put("info", new ArrayList());
        }
        return ret;
    }

    @RequestMapping(value = "rest/original/post/{classId}/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object getOriginalInfo(@PathVariable("classId") String classId, @PathVariable("id") Integer id) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (!AuthUtil.newInstance().isOk()) {
            logger.error("试用已到期");
            ret.put("status", "error");
            ret.put("info", null);
            return ret;
        }

        try {

            RestOriginalInfoVO info = originalService.getInfoByIdAndClassId(id, classId);
            if (info != null) {
                ret.put("status", "success");
                ret.put("info", info);
            } else {
                ret.put("status", "error");
                ret.put("info", info);
            }
        } catch (Exception e) {
            ret.put("status", "error");
            ret.put("info", null);
        }
        return ret;
    }

    @RequestMapping(value = "rest/matts/post/{originalId}", method = RequestMethod.POST)
    @ResponseBody
    public Object getMatts(@PathVariable("originalId") Integer originalId) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (!AuthUtil.newInstance().isOk()) {
            logger.error("试用已到期");
            ret.put("status", "error");
            ret.put("info", null);
            return ret;
        }

        try {
            ret.put("status", "success");
            List<RestMattsInfoVO> info = mattsService.getInfoByOriginalId(originalId);
            ret.put("info", info);
        } catch (Exception e) {
            ret.put("status", "error");
            ret.put("info", new ArrayList());
        }
        return ret;
    }

    @RequestMapping(value = "rest/star/post/{originalId}", method = RequestMethod.POST)
    @ResponseBody
    public Object getStars(@PathVariable("originalId") Integer originalId) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (!AuthUtil.newInstance().isOk()) {
            logger.error("试用已到期");
            ret.put("status", "error");
            ret.put("info", null);
            return ret;
        }

        try {
            ret.put("status", "success");
            List<RestStarInfoVO> info = starService.getInfoByOriginalId(originalId);
            ret.put("info", info);
        } catch (Exception e) {
            ret.put("status", "error");
            ret.put("info", new ArrayList());
        }
        return ret;
    }

    /**
     * 查看是否服务是否存在
     * @return
     */
    @RequestMapping(value = "rest/checkserver/post", method = RequestMethod.POST)
    @ResponseBody
    public Object checkServer() {
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("status", "success");

        return ret;
    }

    /**
     * 使用IP地址注册最后一个使用orginalId
     * @param originalId
     * @return
     */
    @RequestMapping(value = "rest/register/setval/{originalId}", method = RequestMethod.GET)
    @ResponseBody
    public Object registerVal(@PathVariable("originalId") Integer originalId, HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<String, Object>();
        try {
            registerService.saveInfo(IpUtil.getIpAddress(request), originalId);
            ret.put("status", "success");
        } catch (Exception e) {
            ret.put("status", "error");
        }
        return ret;
    }

    /**
     * 使用IP地址注册取得最后一个使用orginalId
     * @return
     */
    @RequestMapping(value = "rest/register/getval", method = RequestMethod.GET)
    @ResponseBody
    public Object getRegisterVal(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<String, Object>();
        try {
            ret.put("status", "success");
            String ip = IpUtil.getIpAddress(request);
            System.out.println(ip);
            Register info = registerService.getInfo(ip);
            if (info != null) {
                ret.put("idvalue", info.getIdvalue());
            } else {
                ret.put("idvalue", 0);
            }
        } catch (Exception e) {
            ret.put("status", "error");
            ret.put("idvalue", 0);
        }
        return ret;
    }
}
